package com.tianji.learning.controller;

import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.domain.query.PageQuery;
import com.tianji.learning.domain.dto.LearningPlanDTO;
import com.tianji.learning.domain.vo.LearningLessonVO;
import com.tianji.learning.domain.vo.LearningPlanPageVO;
import com.tianji.learning.service.ILearningLessonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "我的课表相关接口")
@RestController
@RequestMapping("/lessons")
@RequiredArgsConstructor
public class LearningLessonController {

    private final ILearningLessonService lessonService;

    /**
     * 1、分页查询我的课表
     *
     * @param query
     * @return PageDTO<LearningLessonVO>
     */
    @ApiOperation("查询我的课表，排序字段 latest_learn_time:学习时间排序，create_time:购买时间排序")
    @GetMapping("/page")
    public PageDTO<LearningLessonVO> queryMyLessonPage(PageQuery query) {
        return lessonService.queryMyLessonPage(query);
    }

    /**
     * 2、分页查询我的课表
     *
     * @return LearningLessonVO
     */
    @ApiOperation("查询正在学习的课程")
    @GetMapping("/now")
    public LearningLessonVO queryMyCurrentLesson() {
        return lessonService.queryMyCurrentLesson();
    }

    /**
     * 3、删除课表中课程
     *
     * @return LearningLessonVO
     */
    @ApiOperation("删除课表中课程")
    @DeleteMapping("/{courseId}")
    public void deleteMyLessonCourse(@PathVariable Long courseId) {
        lessonService.deleteMyLessonCourse(null, courseId);
    }

    /**
     * 4、校验当前课程是否已经报名
     *
     * @return
     */
    @ApiOperation("校验当前课程是否已经报名")
    @GetMapping("/{courseId}/valid")
    public Long isLessonValid(
            @ApiParam(value = "课程id", example = "1") @PathVariable("courseId") Long courseId) {
        return lessonService.isLessonValid(courseId);
    }

    /**
     * 5、查询指定课程信息
     *
     * @param courseId
     * @return LearningLessonVO
     */
    @GetMapping("/{courseId}")
    @ApiOperation("查询指定课程信息")
    public LearningLessonVO queryLessonByCourseId(
            @ApiParam(value = "课程id", example = "1") @PathVariable("courseId") Long courseId) {
        return lessonService.queryLessonByCourseId(courseId);
    }

    /**
     * 6、统计课程学习人数
     *
     * @param courseId 课程id
     * @return 学习人数
     */
    @GetMapping("/{courseId}/count")
    Integer countLearningLessonByCourse(@ApiParam(value = "课程id", example = "1") @PathVariable("courseId") Long courseId) {
        return lessonService.countLearningLessonByCourse(courseId);
    }

    /**
     * 7、创建我的学习计划
     *
     * @return LearningLessonVO
     */
    @ApiOperation("创建我的学习计划")
    @PostMapping("/plans")
    public void createMyLessonsPlan(@Valid @RequestBody LearningPlanDTO learningPlanDTO) {
        lessonService.createMyLessonsPlan(learningPlanDTO);
    }

    /**
     * 8、查询我的学习计划
     * @Param query
     * @return PageDTO<LearningPlanVO>
     */
    @ApiOperation("查询我的学习计划")
    @GetMapping("/plans")
    public LearningPlanPageVO queryMyLessonsPlan(PageQuery query) {
        return lessonService.queryMyLessonsPlan(query);
    }

}
